package com.pureland.demo.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pureland.boot.ext.NoRepeatSubmit;
import com.pureland.common.entity.R;
import com.pureland.common.exception.BizException;
import com.pureland.demo.domain.entity.Goods;
import com.pureland.demo.domain.req.GoodsQueryDTO;
import com.pureland.demo.service.IDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @projectName: pureland_cloud
 * @className: com.pureland.demo.controller.DemoController
 * @description: 接口演示
 * @author: tong.li
 * @createTime: 2020/10/12 10:05
 * @version: v1.0
 * @copyright: 版权所有 李彤 © 2020
 */
@RestController
@Validated
@Api(value = "测试API", description = "测试API", tags = "0.0.1")
public class DemoController {

    Logger log = LoggerFactory.getLogger(DemoController.class);

    @Autowired
    private IDemoService service;

    /**
     * 简单的Get请求
     * @return
     */
    @GetMapping("/say")
    public R say1() {
        return R.ok("我是李彤,你好Docker");
    }




    /**
     * 简单的Get请求,带请求参数的
     * @return
     */
    @GetMapping("/who")
    public R say2(String name) {
        return R.ok(String.format("我是%s,你好",name));
    }



    /**
     * Get请求：测试全局异常处理是否生效
     * @return
     */
    @GetMapping("/f")
    public R f() {
        throw new BizException("我把自己搞失败了");
    }

    /**
     * post表单请求示例
     * 注解@NoRepeatSubmit 防止表单重复提交,自定义失效时间
     * @return
     */
    @ApiOperation(value = "商品添加(表单提交)")
    @PostMapping("/addFormData")
    @NoRepeatSubmit(duration = 3, unit = TimeUnit.SECONDS)
    public R addFormData(@Valid Goods goods) {
        LocalDateTime now = LocalDateTime.now();
        goods.setCreatedBy(1L);
        goods.setCreatedName("李彤");
        goods.setCreatedTime(now);
        goods.setModifiedBy(goods.getCreatedBy());
        goods.setModifiedName(goods.getCreatedName());
        goods.setModifiedTime(now);
        goods.setDeleted(false);
       return R.ok(service.save(goods));
    }

    /**
     * post json请求示例
     * 注解@NoRepeatSubmit 防止表单重复提交,默认1秒失效时间
     * @return
     */
    @ApiOperation(value = "商品添加(JSON提交)")
    @PostMapping("/addJson")
    @NoRepeatSubmit
    public R add(@Validated @RequestBody  Goods goods) {
        LocalDateTime now = LocalDateTime.now();
        goods.setCreatedBy(1L);
        goods.setCreatedName("李彤");
        goods.setCreatedTime(now);
        goods.setModifiedBy(goods.getCreatedBy());
        goods.setModifiedName(goods.getCreatedName());
        goods.setModifiedTime(now);
        goods.setDeleted(false);
        return R.ok(service.save(goods));
    }

    /**
     * post json请求示例 修改
     * @return
     */
    @PostMapping("/update")
    public R update(@Valid @RequestBody  Goods goods) {
        if (goods.getId() == null) {
            throw new BizException("修改ID不能为空");
        }
        return R.ok(service.updateById(goods));
    }



    /**
     * 根据ID查询Goods
     * @return
     */
    @GetMapping("/get")
    public R getGoodsById(@RequestParam Long id) {
        return R.ok(service.getById(id));
    }


    /**
     * 根据ID查询Goods
     * @return
     */
    @PostMapping("/list")
    public R getGoodsList(@RequestBody GoodsQueryDTO queryDTO) {
        log.info("这里我打印了INFO的日志");
        log.debug("这里我打印了DEBUG的日志");
        return R.ok(service.getGoodsList(queryDTO));
    }


    /**
     *  逻辑批量删除
     * @return
     */
    @PostMapping("/batchDelete")
    public R batchDelete( @NotEmpty(message = "删除的列表不能为空")    @RequestBody List<Long> ids) {
        // 删除操作谨慎起见,程序要更加严谨,再次校验,防止删库
        if (CollectionUtils.isEmpty(ids)) {
            return R.fail("删除失败");
        }
        Goods goods = new Goods();
        goods.setDeleted(true);
        return R.ok(service.update(goods, Wrappers.<Goods>lambdaUpdate()
                .in(Goods::getId,ids)));
    }





}
